package com.ys.nms.module.business.controller.admin.redData;

import com.ys.nms.framework.common.util.date.DateUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ICMP性能数据专用解析器
 */
@Component
@AllArgsConstructor
public class IcmpPerfDataParser {

    private static final List<String> ICMP_COLUMNS = Arrays.asList(
            "avg", "xmt", "rcv", "min", "max"
    );

    private final RrdMultiColumnReader rrdMultiReader;

    /**
     * 解析ICMP性能数据
     */
    public RrdMultiQueryResult parseIcmpData(RrdQueryParams params) {
        RrdMultiQueryResult result = rrdMultiReader.queryMultiColumnRrdData(params);
        // 可以在这里添加ICMP特定的数据处理逻辑
        List<String> dateTime = new ArrayList<>();
        List<Double> data = new ArrayList<>();
        result.getDataPoints().forEach(dp -> {
            Long timestamp = dp.getTimestamp();
            String s = TimestampConverter.timestampToDefaultString(timestamp);
            try {
                double avg = Double.parseDouble(dp.getMetric("avg"));
                dateTime.add(s);
                data.add(avg);
            } catch (Exception e) {

            }

        });

        result.setData(data);
        result.setDateTime(dateTime);

        if (result.getSuccess()) {
            System.out.println("ICMP数据解析完成:" +
                    result.getTotalPoints() + result.getColumnNames());
        }

        return result;
    }

    /**
     * 获取ICMP延迟统计
     */
//    public Map<String, Object> getIcmpLatencyStats(RrdMultiQueryResult result) {
//        return rrdMultiReader.calculateColumnStats(result, "avg");
//    }

    /**
     * 获取丢包率统计
     */
    public Map<String, Object> getPacketLossStats(RrdMultiQueryResult result) {
        // 计算丢包率: (发送-接收)/发送
        List<Double> packetLossRates = new ArrayList<>();

        for (RrdMultiDataPoint point : result.getDataPoints()) {
            if (point.isMetricValid("xmt") && point.isMetricValid("rcv")) {
                String sent = point.getMetric("xmt");
                String received = point.getMetric("rcv");
                if (Double.valueOf(sent) > 0) {
                    double lossRate = (Double.valueOf(sent) - Double.valueOf(received)) / Double.valueOf(sent) * 100; // 百分比
                    packetLossRates.add(lossRate);
                }
            }
        }

        if (packetLossRates.isEmpty()) {
            return Map.of("count", 0, "message", "无法计算丢包率");
        }

        double avgLoss = packetLossRates.stream().mapToDouble(Double::doubleValue).average().orElse(0);
        double maxLoss = packetLossRates.stream().mapToDouble(Double::doubleValue).max().orElse(0);

        return Map.of(
                "count", packetLossRates.size(),
                "averageLossRate", String.format("%.2f%%", avgLoss),
                "maxLossRate", String.format("%.2f%%", maxLoss)
        );
    }
}
